//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web.Security;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security.Membership;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;
namespace DotNetNuke.Modules.Admin.Users
{
	partial class Password : UserModuleBase
	{
		public delegate void PasswordUpdatedEventHandler(object sender, PasswordUpdatedEventArgs e);
		public event PasswordUpdatedEventHandler PasswordUpdated;
		public event PasswordUpdatedEventHandler PasswordQuestionAnswerUpdated;
		public UserMembership Membership {
			get {
				UserMembership _Membership = null;
				if (User != null) {
					_Membership = User.Membership;
				}
				return _Membership;
			}
		}
		public void OnPasswordUpdated(PasswordUpdatedEventArgs e)
		{
			if (PasswordUpdated != null) {
				PasswordUpdated(this, e);
			}
		}
		public void OnPasswordQuestionAnswerUpdated(PasswordUpdatedEventArgs e)
		{
			if (PasswordQuestionAnswerUpdated != null) {
				PasswordQuestionAnswerUpdated(this, e);
			}
		}
		public override void DataBind()
		{
			if (IsAdmin) {
				lblTitle.Text = string.Format(Localization.GetString("PasswordTitle.Text", LocalResourceFile), User.Username, User.UserID.ToString());
			} else {
				trTitle.Visible = false;
			}
			lblLastChanged.Text = User.Membership.LastPasswordChangeDate.ToLongDateString();
			if (User.Membership.UpdatePassword) {
				lblExpires.Text = Localization.GetString("ForcedExpiry", this.LocalResourceFile);
			} else {
				if (PasswordConfig.PasswordExpiry > 0) {
					lblExpires.Text = User.Membership.LastPasswordChangeDate.AddDays(PasswordConfig.PasswordExpiry).ToLongDateString();
				} else {
					lblExpires.Text = Localization.GetString("NoExpiry", this.LocalResourceFile);
				}
			}
			if (((!MembershipProviderConfig.PasswordRetrievalEnabled) && IsAdmin && (!IsUser))) {
				pnlChange.Visible = false;
			} else {
				pnlChange.Visible = true;
				if (IsAdmin && !IsUser) {
					lblChangeHelp.Text = Localization.GetString("AdminChangeHelp", this.LocalResourceFile);
					trOldPassword.Visible = false;
				} else {
					lblChangeHelp.Text = Localization.GetString("UserChangeHelp", this.LocalResourceFile);
				}
			}
			if (!MembershipProviderConfig.PasswordResetEnabled) {
				pnlReset.Visible = false;
			} else {
				pnlReset.Visible = true;
				if (IsAdmin && !IsUser) {
					if (MembershipProviderConfig.RequiresQuestionAndAnswer) {
						pnlReset.Visible = false;
					} else {
						lblResetHelp.Text = Localization.GetString("AdminResetHelp", this.LocalResourceFile);
					}
					trQuestion.Visible = false;
					trAnswer.Visible = false;
				} else {
					if (MembershipProviderConfig.RequiresQuestionAndAnswer && IsUser) {
						lblResetHelp.Text = Localization.GetString("UserResetHelp", this.LocalResourceFile);
						lblQuestion.Text = User.Membership.PasswordQuestion;
						trQuestion.Visible = true;
						trAnswer.Visible = true;
					} else {
						pnlReset.Visible = false;
					}
				}
			}
			if (MembershipProviderConfig.RequiresQuestionAndAnswer && IsUser) {
				pnlQA.Visible = true;
			} else {
				pnlQA.Visible = false;
			}
		}
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			ClientAPI.RegisterKeyCapture(this.Parent, this.cmdUpdate.Controls[0], 13);
			ClientAPI.RegisterKeyCapture(this, this.cmdUpdate.Controls[0], 13);
			cmdReset.Click += new EventHandler(cmdReset_Click);
			cmdUpdate.Click += new EventHandler(cmdUpdate_Click);
			cmdUpdateQA.Click += new EventHandler(cmdUpdateQA_Click);
		}
		private void cmdReset_Click(object sender, System.EventArgs e)
		{
			string answer = "";
			if (MembershipProviderConfig.RequiresQuestionAndAnswer && !IsAdmin) {
				if (String.IsNullOrEmpty(txtAnswer.Text)) {
					OnPasswordUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.InvalidPasswordAnswer));
					return;
				}
				answer = txtAnswer.Text;
			}
			try {
				UserController.ResetPassword(User, answer);
				OnPasswordUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.Success));
			} catch (ArgumentException exc) {
				OnPasswordUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.InvalidPasswordAnswer));
			} catch (Exception ex) {
				OnPasswordUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.PasswordResetFailed));
			}
		}
		private void cmdUpdate_Click(System.Object sender, System.EventArgs e)
		{
			if (txtNewPassword.Text != txtNewConfirm.Text) {
				OnPasswordUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.PasswordMismatch));
				return;
			}
			if (!UserController.ValidatePassword(txtNewPassword.Text)) {
				OnPasswordUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.PasswordInvalid));
				return;
			}
			if (!IsAdmin && String.IsNullOrEmpty(txtOldPassword.Text)) {
				OnPasswordUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.PasswordMissing));
				return;
			}
			if (!IsAdmin && txtNewPassword.Text == txtOldPassword.Text) {
				OnPasswordUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.PasswordNotDifferent));
				return;
			}
			try {
				if (UserController.ChangePassword(User, txtOldPassword.Text, txtNewPassword.Text)) {
					OnPasswordUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.Success));
				} else {
					OnPasswordUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.PasswordResetFailed));
				}
			} catch (MembershipPasswordException mex) {
				OnPasswordUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.InvalidPasswordAnswer));
			} catch (Exception ex) {
				OnPasswordUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.PasswordResetFailed));
			}
		}
		private void cmdUpdateQA_Click(object sender, System.EventArgs e)
		{
			if (String.IsNullOrEmpty(txtQAPassword.Text)) {
				OnPasswordQuestionAnswerUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.PasswordInvalid));
				return;
			}
			if (String.IsNullOrEmpty(txtEditQuestion.Text)) {
				OnPasswordQuestionAnswerUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.InvalidPasswordQuestion));
				return;
			}
			if (String.IsNullOrEmpty(txtEditAnswer.Text)) {
				OnPasswordQuestionAnswerUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.InvalidPasswordAnswer));
				return;
			}
			UserInfo objUser = UserController.GetUserById(PortalId, UserId);
			if (UserController.ChangePasswordQuestionAndAnswer(objUser, txtQAPassword.Text, txtEditQuestion.Text, txtEditAnswer.Text)) {
				OnPasswordQuestionAnswerUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.Success));
			} else {
				OnPasswordQuestionAnswerUpdated(new PasswordUpdatedEventArgs(PasswordUpdateStatus.PasswordResetFailed));
			}
		}
		public class PasswordUpdatedEventArgs
		{
			private PasswordUpdateStatus _UpdateStatus;
			public PasswordUpdatedEventArgs(PasswordUpdateStatus status)
			{
				_UpdateStatus = status;
			}
			public PasswordUpdateStatus UpdateStatus {
				get { return _UpdateStatus; }
				set { _UpdateStatus = value; }
			}
		}
	}
}
